package com.hiray.taglayoutmanager;

import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hiray.taglayoutmanager.uitility.ClickableViewHolder;

import java.util.Random;

/**
 * Created by hiray on 2017/12/18.
 *
 * @author hiray
 */

public class StaggeredGridAdapter extends RecyclerView.Adapter<ClickableViewHolder> {

    private Random random = new Random(System.currentTimeMillis());
    private LayoutInflater layoutInflater;

    @Override
    public ClickableViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (layoutInflater == null) {
            layoutInflater = LayoutInflater.from(parent.getContext());
        }
        View rootView = layoutInflater.inflate(R.layout.fix_width_item, parent, false);
        rootView.setMinimumHeight(random.nextInt(270) + 30);
        return new ClickableViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(ClickableViewHolder holder, int position) {
        int i = position % 3;
        if (i == 0)
            holder.itemView.setBackgroundResource(R.color.colorAccent);
        else if (i == 2) holder.itemView.setBackgroundResource(R.color.colorPrimary);
        else {
            holder.itemView.setBackgroundResource(R.color.colorPrimaryDark);
        }
        ((AppCompatTextView) holder.itemView).setText(String.valueOf(position));
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        random.setSeed(System.currentTimeMillis());
    }

    @Override
    public int getItemCount() {
        return 28;
    }
}
